package com.dream.controller;

import com.alibaba.fastjson.JSON;
import com.dream.pojo.Bulletin;
import com.dream.pojo.BulletinIdInformation;
import com.dream.pojo.User;
import com.dream.service.BulletinService;
import com.dream.service.impl.BulletinServiceImpl;
import com.dream.utils.DateUtil;
import com.dream.utils.PageUtil;
import com.dream.utils.ResultCommon;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/BulletinController.do")
public class BulletinController extends BaseServlet{
    BulletinService bulletinService = new BulletinServiceImpl();

    /**
     * Ajax
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void add(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        HttpSession session = request.getSession();
        ResultCommon resultCommon = null;
        User loginUser = (User) session.getAttribute("loginUser");

        String bulletinTitle = request.getParameter("bulletinTitle");
        String bulletinImg = request.getParameter("bulletinImg");
        String bulletinContent = request.getParameter("bulletinContent");

        long publisher = loginUser.getId();//当前登录人
        long gmtCreate = System.currentTimeMillis();
        long gmtModify = System.currentTimeMillis();
        Bulletin bulletin = new Bulletin(bulletinTitle,"/upload/"+bulletinImg,bulletinContent,publisher,gmtCreate,gmtModify);
        int count = bulletinService.addBulletin(bulletin);
        if (count>0){
            resultCommon=new ResultCommon(200,"新增成功",null);
        }else {
            resultCommon=new ResultCommon(500,"新增异常",null);
        }
        String toJSONString = JSON.toJSONString(resultCommon);
        response.getWriter().write(toJSONString);
    }

    public void show(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        List<Bulletin> bulletins = bulletinService.list();
        ResultCommon resultCommon = new ResultCommon(200,"请求成功",bulletins);
        String toJSONString = JSON.toJSONString(resultCommon);
        response.getWriter().write(toJSONString);
    }

    public void findById(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        String bulletinId = request.getParameter("bulletinId");
        Bulletin bulletin = bulletinService.findById(Long.valueOf(bulletinId));
        long gmtCreate = bulletin.getGmtCreate();

        Date date = new Date();
        date.setTime(gmtCreate);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        request.setAttribute("bulletin",bulletin);
        request.setAttribute("createTime",dateFormat.format(date));

        //点击数+1
        bulletinService.updateReadCount(Long.valueOf(bulletinId));

        request.getRequestDispatcher("viewBulletin.jsp").forward(request,response);
    }

    /**
     * 分页查询
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void list(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        String bulletinTitle = request.getParameter("bulletinTitle");//条件1
        String userName = request.getParameter("userName");//条件2
        String startTime = request.getParameter("startTime");//条件3
        String endTime = request.getParameter("endTime");//条件4

        String pageIndex = request.getParameter("pageIndex");//条件3 当前页码
        if (pageIndex==null||"".equals(pageIndex)){
            pageIndex="1";//默认值
        }
        String pageSize = request.getParameter("pageSize");//条件5 页面大小
        if (pageSize==null||"".equals(pageSize)){
            pageSize="5";//默认值
        }
        //条数
        long totalCount = bulletinService.totalCount(bulletinTitle,userName,startTime,endTime);
        //每页条数
        List<BulletinIdInformation> bulletinIdInfomationList = bulletinService.list(bulletinTitle,userName,startTime,endTime,Long.valueOf(pageIndex),Long.valueOf(pageSize));
        for (BulletinIdInformation bulletinIdInformation: bulletinIdInfomationList) {
            bulletinIdInformation.setGmtCreateTime(DateUtil.formateTime(bulletinIdInformation.getGmtCreate()));
            bulletinIdInformation.setGmtModifyTime(DateUtil.formateTime(bulletinIdInformation.getGmtModify()));
        }
        //封装一个分页的工具类
        PageUtil<BulletinIdInformation> pageUtil = new PageUtil<BulletinIdInformation>(Long.valueOf(pageIndex),Long.valueOf(pageSize),totalCount,bulletinIdInfomationList);
        //存值转发
        request.setAttribute("pageUtil",pageUtil);
        request.setAttribute("bulletinTitle",bulletinTitle);//回显搜索条件
        request.setAttribute("userName",userName);//回显搜索条件
        request.setAttribute("startTime",startTime);//回显搜索条件
        request.setAttribute("endTime",endTime);//回显搜索条件
        request.getRequestDispatcher("viewBulletinInfo.jsp").forward(request,response);
    }

    public void delete(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        String bulletinId = request.getParameter("bulletinId");
        int count = bulletinService.deleteById(Long.valueOf(bulletinId));
        if (count>0){
            response.getWriter().write("<script>alert('公告删除成功');location.href='/BulletinController.do?action=list';</script>");
        }else {
            response.getWriter().write("<script>alert('公告刪除失敗');location.href='/BulletinController.do?action=list';</script>");
        }
    }

    public void goupdate(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        String bulletinId = request.getParameter("bulletinId");
        request.setAttribute("bulletinId",bulletinId);
        request.getRequestDispatcher("modifyBulletinInfo.jsp").forward(request,response);
    }

    public void getBulletinById(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        ResultCommon resultCommon = null;
        try {
            String bulletinId = request.getParameter("bulletinId");
            Bulletin bulletin = bulletinService.findById(Long.valueOf(bulletinId));
            resultCommon = new ResultCommon(200,"请求成功",bulletin);
        }catch (Exception e){
            e.printStackTrace();
            resultCommon=new ResultCommon(500,"请求失败",null);
        }
        String jsonString = JSON.toJSONString(resultCommon);
        response.getWriter().write(jsonString);
    }

    public void update(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        HttpSession session = request.getSession();
        ResultCommon resultCommon = null;
        User loginUser = (User) session.getAttribute("loginUser");

        String bulletinId = request.getParameter("bulletinId");
        String bulletinTitle = request.getParameter("bulletinTitle");
        String bulletinImg = request.getParameter("bulletinImg");
        String bulletinContent = request.getParameter("bulletinContent");

        long publisher = loginUser.getId();//当前登录人
        long gmtModify = System.currentTimeMillis();
        Bulletin bulletin = new Bulletin(Long.valueOf(bulletinId),bulletinTitle, "/upload/"+bulletinImg, bulletinContent, publisher, gmtModify);
        int count = bulletinService.updateBulletin(bulletin);
        if (count > 0) {
            resultCommon=new ResultCommon(200,"更新成功",null);
        } else {
            resultCommon=new ResultCommon(500,"更新异常",null);
        }
        String toJSONString = JSON.toJSONString(resultCommon);
        response.getWriter().write(toJSONString);
    }
}
